/**
 * 
 */
package com.acme.task1.calculator;

import com.acme.task1.calculator.operation.Diff;
import com.acme.task1.calculator.operation.Div;
import com.acme.task1.calculator.operation.Mult;
import com.acme.task1.calculator.operation.Operatable;
import com.acme.task1.calculator.operation.Sum;

/**
 * @author nulls.narik
 *
 */
public class Calculation {

	private final static String MESSAGE_ERROR_COUNT_OF_ARGUMENTS = "Wrong count of argument. Must be 3!";
	
	private final static String MESSAGE_ERROR_ARGUMENT_TWO = "Wrong second argument. Must be *,/,+,-!";
	
	private final static String MESSAGE_ERROR_PARSE_ARGUMENTS = "Error when try parse arguments";
	
	private Operatable getOperation(char op) {
		switch (op) {
			case '*':
			case '#':
				return new Mult();
			case '/':
				return new Div();
			case '-':
				return new Diff();
			case '+':
				return new Sum();
			default:
				return null;
		}
	}

	public double calc(String[] args) throws Exception {
		if (args.length != 3) {
			throw new Exception(MESSAGE_ERROR_COUNT_OF_ARGUMENTS);
		}
		
		if (args[1].length() == 0) {
			throw new Exception(MESSAGE_ERROR_ARGUMENT_TWO);
		}
		
		Operatable operation = getOperation(args[1].charAt(0));
		if (operation == null) {
			throw new Exception(MESSAGE_ERROR_ARGUMENT_TWO);
		}
		
		double arg1 = 0, arg2 = 0;
		try {
			arg1 = Double.parseDouble(args[0]);
			arg2 = Double.parseDouble(args[2]);
		} catch (NumberFormatException exception) {
			throw new Exception(MESSAGE_ERROR_PARSE_ARGUMENTS);
		}
		
		return operation.calc(arg1, arg2);
	}
}
